package com.login;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String email=request.getParameter("email");
		String password=request.getParameter("password");
		System.out.println(email+"进入LoginServlet.doGet()方法"+password);
		UserInfo user=new UserInfo();
		user.setEmail(email);
		user.setPassword(password);
		LoginImpl login=new LoginImpl();
		JSONObject message=new JSONObject();
		if(login.serachUser(user)){
			if(login.checkPw(user)){
				message.put("message", "登陆成功！");
				request.getSession().setAttribute("uId", login.getId(user));
				request.getSession().setAttribute("uEmail", user.getEmail());
				request.getSession().setAttribute("uPw", user.getPassword());
			}
			else{
				message.put("message", "输入密码有误！请核对正确后再输入。");
			}
		}
		else{
			message.put("message", "账号不存在！请重新输入。");
		}
		response.getWriter().print(message);  
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	
	}

}
